package com.moli.lumpSugar.user.consumer;


import cn.hutool.core.bean.BeanUtil;
import com.moli.lumpSugar.common.cache.user.UserCache;
import com.moli.lumpSugar.common.constant.MQConstant;
import com.moli.lumpSugar.common.domain.dto.PushMessageDTO;
import com.moli.lumpSugar.common.domain.vo.response.WSLoginSuccessMQMsg;
import com.moli.lumpSugar.transaction.service.PushService;
import com.moli.lumpSugar.user.dao.UserDao;
import com.moli.lumpSugar.user.domain.entity.User;
import com.moli.lumpSugar.user.domain.enums.ActiveStatusEnum;
import com.moli.lumpSugar.user.event.LoginSuccessEvent;
import com.moli.lumpSugar.user.service.IpService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * Description: 用户登录成功后，处理发送的MQ消息
 * Author: zhangjt
 * Date: 2024-11-06
 */
@RocketMQMessageListener(consumerGroup = MQConstant.LOGIN_MSG_GROUP, topic = MQConstant.LOGIN_MSG_TOPIC)
@Component
public class LoginSuccessEventConsumer implements RocketMQListener<WSLoginSuccessMQMsg> {

    @Autowired
    private UserDao userDao;
    @Autowired
    private IpService ipService;
    @Autowired
    private UserCache userCache;

    @Override
    public void onMessage(WSLoginSuccessMQMsg dto) {
        User updateInfoUser = new User();
        BeanUtil.copyProperties(dto, updateInfoUser);
        updateInfoUser.setActiveStatus(ActiveStatusEnum.ONLINE.getType());
        userDao.updateById(updateInfoUser);
        ipService.refreshIpDetailAsync(dto.getId());
        userCache.online(dto.getId(), dto.getLastOptTime());
    }


}
